import React, { PureComponent } from 'react';
import _ from 'lodash';
import { connect } from 'dva';
import { message, Spin } from 'antd';
import style from '../index.less';
import { FirstHeaderC, ModalC, PrimaryC, PrimaryGhostC } from '../../../../comps';
import UploadTableC from './uploadTableC';
import CommonFormC from './commonForm';
import { isRepeat } from '../../../../utils/utils';
@connect(({ CollectingMonitoringM, userM: user, loading }) => ({ CollectingMonitoringM, user, loading }))
export default class AddPortfolioC extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      detailData: undefined, //详情数据
    };
  }
  componentDidMount() {
    if (this.props.id) {
      this.queryDetailById(); //查询详情
    }
  }
  UNSAFE_componentWillReceiveProps(nextProps) {}
  //确定提交数据
  addOK = () => {
    const { dispatch } = this.props;
    let common = this.state.CommonForm.getFormData();
    let uploadData = this.state.uploadC.getTableData();
    if (common && uploadData) {
      //标的要素和通用要素校验成功 进行提交
      const arr = _.cloneDeep(uploadData);
      let tempArr = [];
      arr.map((item) => {
        tempArr.push(item.exchangeCode + '-' + item.tradingCode);
      });
      if (isRepeat(tempArr)) {
        message.warning('标的要素中标的代码不可重复');
        return;
      }
      arr.map((item) => {
        delete item.id;
        delete item.isEdit;
        item.industry = item.industry == null ? '' : item.industry;
      });
      dispatch({
        type: 'CollectingMonitoringM/addEditPortfolio',
        payload: {
          ...common,
          items: arr,
          portfolioId: this.state.detailData ? this.state.detailData.portfolioId : undefined,
        },
        callback: (res) => {
          if (res) {
            //提交成功 通知主页面刷新
            this.props.onCancel('ok');
          }
        },
      });
    }
  };

  //查询详情
  queryDetailById = () => {
    const { dispatch } = this.props;
    dispatch({
      type: 'CollectingMonitoringM/queryDetailById',
      payload: {
        productId: this.props.id,
      },
      callback: (data) => {
        if (data) {
          data.items &&
            data.items.map((item) => {
              item.weight = Number(item.weight);
            });
          this.setState({
            detailData: data || {},
          });
        }
      },
    });
  };
  render() {
    const { id, visible } = this.props;
    const { detailData } = this.state;
    const modalProps = {
      visible: visible,
      // confirmLoading:loading.effects['initialSplitM/uploadFile'] === true,
      onOk: () => {
        this.addOK();
      },
      onCancel: () => {
        this.props.onCancel();
      },
      title: id ? '编辑模拟组合' : '新建模拟组合',
      className: style.addPortfolio,
      // height: 220, //内容区域的高度
      width: 920,
    };
    return (
      <ModalC {...modalProps}>
        <FirstHeaderC title='通用要素' />
        <CommonFormC onRef={(ref) => (this.state.CommonForm = ref)} info={detailData} type={id ? 'add' : 'edit'} />
        <FirstHeaderC title='标的要素' />
        <UploadTableC onRef={(ref) => (this.state.uploadC = ref)} defaultData={detailData ? detailData.items : []} type={id ? 'add' : 'edit'} />
      </ModalC>
    );
  }
}
